<template>
  <el-form ref="deptForm" :model="form" :rules="relus" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in list" :key="item.id" :value="item.username" :label="item.username" />

      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getememm } from '@/api/employees'
import { addlist, editlist, editreallist } from '@/api/department'
export default {
  name: 'VueHrDepartdialog',
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originlist: {
      type: Array,
      required: true
    }
  },
  data() {
    // 部门编码非重判断
    const validcode = (rule, value, callback) => {
      let origincode = this.originlist.map(item => item.code)
      console.log(origincode)
      if (this.isEdit) {
        origincode = this.originlist.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (origincode.includes(value)) {
        callback(new Error(`编码${value}已经存在`))
      } else {
        callback()
      }
    }
    const validname = (rule, value, callback) => {
      let originname = this.originlist.filter(item => item.pid !== this.id).map(item => item.name)
      if (this.isEdit) {
        const pid = this.originlist.find(item => item.id === this.id).pid // 找自己的pid
        const brothers = this.originlist.filter(item => item.pid === pid) // 根据pid 找兄弟
        originname = brothers.filter(item => item.id !== this.id).map(item => item.name) // 排除自己
      }
      if (originname.includes(value)) {
        callback(new Error(`该部门下的${value}已经存在`))
      } else {
        callback()
      }
    }
    return {
      list: [],
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      relus: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { required: true, min: 1, max: 50, message: '请输入1-50的字符串', trigger: 'blur' },
          { validator: validname, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { required: true, min: 1, max: 50, message: '请输入1-50的字符串', trigger: 'blur' },
          { validator: validcode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: 'change' }

        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { required: true, min: 1, max: 50, message: '请输入1-300的字符串', trigger: 'blur' }
        ]
      }
    }
  },

  mounted() {

  },
  created() {
    this.getemployees()
    if (this.isEdit) {
      this.hEdit()
      this.HrealEdit()
    }
  },

  methods: {
    hSubmit() {
      this.$refs.deptForm.validate((valid) => {
        this.isEdit ? this.HrealEdit() : this.Hadd()
        this.$emit('Hshow')
      })
    },
    hCancel() {
      this.$emit('Hshow')
    },
    async getemployees() {
      const res = await getememm()
      console.log(res)
      this.list = res.data
    },
    // 获取新增部门的axios
    async Hadd() {
      try {
        const res = await addlist({ ...this.form, pid: this.id })
        console.log(res)
        this.getemployees()
      } catch (error) {
        console.log(error)
      }
    },
    // 根据id获取部门详情  编辑使用
    async hEdit() {
      try {
        const res = await editlist(this.id)
        console.log(res)
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 根据id修改部门信息
    async HrealEdit() {
      try {
        const res = await editreallist(this.form)
        this.getemployees()
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
